$(function () {
    $("#jqGrid").Grid({
        url: '../modulespstock/list',
        colModel: [
            {label: '仓库名称', name: 'storeHouseName', index: 'STORE_ID'},
            {label: '型芯编码', name: 'partCode', index: 'PART_CODE'},
            {label: '型芯备件码', name: 'uniqueCode', index: 'UNIQUE_CODE'},
            {label: '型芯名称', name: 'sparePartsName', index: 'SPARE_PARTS_NAME'},
            {label: '图号', name: 'sparePartsSpec', index: 'SPARE_PARTS_SPEC'},
            {
                label: '库存状态', name: 'state', index: 'STATE',
                formatter: function (value) {
                    return sysEnums['MODULE_SPAREPART_STOCK_STATE'][value];
                }
            },
            {
                label: '入库时间', name: 'stockInDate', index: 'STOCK_IN_DATE',
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss')
                }
            },
            {label: '备注', name: 'remark', index: 'remark'},
            {label: '采购批次ID', name: 'purchasingBatchId', hidden: true},
            {label: 'id', name: 'id', key: true, hidden: true},
        ]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        moduleSpStock: {
            remark: '',
            delFlag: '',
            stockOutDate: '',
            stockInDate: '',
            state: '',
            uniqueCode: '',
            purchasingBatchId: '',
            id: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            storeId: '',
            sparePartsName: '',
            uniqueBarCode: '',
            batchMonth: '', // 采购批次月份
            state: 1
        },
        storeHouseList: [],// 仓库列表
        moduleSparePartsStockStateList: enumsToList('MODULE_SPAREPART_STOCK_STATE') //型芯备件库存状态
    },
    created(){
        this.loadStoreHouseList();
    },
    methods: {
        loadStoreHouseList: function (){
            Ajax.request({
                url: "../storehouse/queryAll?moduleType=3",
                async: true,
                successCallback: res=> {
                    this.storeHouseList = res.list;
                }
            });
        },
        query: function () {
            vm.reload();
        },
        batchStockIn: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 型芯入库',
                content: '../module/modulespstockin.html',
                btn: ['确定入库', '取消'],
                btn1: function (index) {
                    // 执行入库操作
                    Ajax.request({
                        url: "../modulespstock/confirmStockIn",
                        params: {},
                        type: "POST",
                        successCallback: function (r) {
                            // 关闭当前页面
                            layer.close(index);
                            alert('入库成功', function (index) {
                                vm.reload();
                            });
                        }
                    });
                }
            })
        },
        batchStockOut: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 型芯出库',
                content: '../module/modulespstockout.html',
                btn: ['确认出库', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var ids = iframeWin.getSelectedPos();
                    if (!ids || ids.length == 0) {
                        iview.Message.error("请至少选择一条记录！");
                        return;
                    }
                    // 执行入库操作
                    Ajax.request({
                        url: "../modulespstock/confirmStockOut",
                        params: JSON.stringify(ids),
                        type: "POST",
                        contentType: "application/json",
                        successCallback: function (r) {
                            // 关闭当前页面
                            layer.close(index);
                            alert('出库成功', function (index) {
                                vm.reload();
                            });
                        }
                    });
                }
            })
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            let url = vm.moduleSpStock.id == (null || '') ? "../modulespstock/save" : "../modulespstock/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.moduleSpStock),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../modulespstock/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../modulespstock/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.moduleSpStock = r.moduleSpStock;
                }
            });
        },
        reload: function (event) {
            // 清空postData缓存数据
            var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
            $.each(postData, function (k, v) {
                delete postData[k];
            });
            // 构建查询参数
            var param = {};
            if (this.q.sparePartsName) {
                param.sparePartsName = this.q.sparePartsName;
            }
            if (this.q.uniqueBarCode) {
                param.uniqueBarCode = this.q.uniqueBarCode;
            }
            if (this.q.batchMonth) {
                param.batchMonth = this.convert2MonthStr(this.q.batchMonth);
            }
            if (this.q.state) {
                param.state = this.q.state;
            }
            if (this.q.storeId) {
                param.storeId = this.q.storeId;
            }
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: param,
                page: page
            }).trigger("reloadGrid");
        },
        reloadSearch: function () {
            vm.q = {
                storeId: '',
                sparePartsName: '',
                uniqueBarCode: '',
                batchMonth: '',
                state: 1
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        /**
         * 日期转化为年月字符类型 yyyyMM-dd
         * @param date
         * @returns {string}
         */
        convert2MonthStr: function (date){
            return date.getFullYear()  + zeroFill(date.getMonth() + 1).toString();
        }
    }
});
